'use strict'

/**-----------------------------------------------------------------------------------------------------
 * 练习插入一个节点，关键点在于构建节点，插入节点与改变节点道理互通
 * `var a = 123;`    -->    `var a = 123, b = 456;`
 **/

const {parse} = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
const generator = require('@babel/generator').default;

let code = `var a = 123;`

const visitor = {
    VariableDeclarator:{
        enter:function(path){
            if(path.node.id.name == 'a'){
                path.insertAfter(t.VariableDeclarator(t.identifier('b'), t.numericLiteral(456)));
            }
        },
    },
}

let ast = parse(code);
traverse(ast, visitor);
console.log(generator(ast).code);
